﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

using System.ComponentModel.DataAnnotations;

using System.ComponentModel;

namespace DDD.Infrastructure.Extend
{
  public static  class EnumExtensions
    {

    

        public static string ToDisplayName(this Enum value)
        {
            var type = value.GetType();
            MemberInfo member = type.GetMember(value.ToString()).FirstOrDefault();
 
            var displayAttribute = member?.GetCustomAttributes<DisplayAttribute>().FirstOrDefault();
            if (displayAttribute != null)
            {
                return displayAttribute.Name;
            }

            var descriptionAttribute = member?.GetCustomAttributes<DescriptionAttribute>().FirstOrDefault();
            if (descriptionAttribute != null)
            {
                return displayAttribute.Description;
            }
            return member?.Name;
        }



    }
}
